package Controllers;

import Information.UserInfo;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.*;

import java.net.URL;
import java.util.ResourceBundle;


public class SignUpController extends Application implements Initializable {
    @FXML
    private TextField nickname; //输入昵称
    @FXML
    private TextField signUpID; //注册账号输入
    @FXML
    private PasswordField signUpPassword; //注册密码输入
    @FXML
    private PasswordField confirmPassword; //确认密码输入
    @FXML
    private AnchorPane SignUp_fxml; //注册界面

    public SignUpController() {
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            AnchorPane root = FXMLLoader.load(getClass().getResource("../Resources/SignUp.fxml"));
            Scene scene = new Scene(root, 288.0, 430.0);
            //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.getIcons().add(new Image("/Images/logoblue.png"));
            primaryStage.setTitle("注册");
            primaryStage.setScene(scene);
            primaryStage.setResizable(false);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void confirmSignUp() throws Exception {
        // 注册账号功能
        boolean canSignUp = true;
        String NicknameText = nickname.getText(); //昵称数据
        String IdText = signUpID.getText();  // 账号数据
        String PasswordText = signUpPassword.getText();  // 密码数据
        String confirmPasswordText = confirmPassword.getText();  // 确认密码数据

        if(nickname.getText().isEmpty() ||
                signUpID.getText().isEmpty() ||
                signUpPassword.getText().isEmpty() ||
                confirmPassword.getText().isEmpty()) {
            //判空输入
            Alert alert = new Alert(Alert.AlertType.INFORMATION,
                    "输入不能为空！",
                    new ButtonType("确定", ButtonBar.ButtonData.YES));
            alert.setHeaderText(null);
            alert.setTitle("提示");
            alert.show();
        }
        else {
            if(PasswordText.equals(confirmPasswordText)) {
                FileReader fr = new FileReader("src/Passwords/UserID.txt");
                BufferedReader bufferedreader = new BufferedReader(fr);
                String UidText;
                while ((UidText = bufferedreader.readLine()) != null) {
                    if (UidText.length() != 0) {
                        if(IdText.equals(UidText)) {
                            Alert alert = new Alert(Alert.AlertType.INFORMATION,
                                    "账号已存在！请勿重复注册！",
                                    new ButtonType("确定", ButtonBar.ButtonData.YES));
                            alert.setHeaderText(null);
                            alert.setTitle("提示");
                            alert.show();
                            canSignUp = false;
                            break;
                        }
                    }
                }
                fr.close();

                if(canSignUp) {
                    // 输入密码相同且账号不重复，开始创建账号文件
                    String user_path = "src/Passwords/" + IdText + ".txt";
                    File UserFile = new File(user_path);

                    // 写入密码
                    FileWriter FW = new FileWriter(UserFile, true);
                    BufferedWriter writePassword = new BufferedWriter(FW);
                    writePassword.write(PasswordText);
                    writePassword.flush();
                    writePassword.close();
                    FW.close();

                    // 账号写入账号库
                    String UID_path = "src/Passwords/UserID.txt";
                    File UIDFile = new File(UID_path);
                    FileWriter fw = new FileWriter(UIDFile, true);
                    BufferedWriter writeUID = new BufferedWriter(fw);
                    writeUID.write(IdText + "\r\n");
                    writeUID.flush();
                    writeUID.close();
                    fw.close();

                    Alert alert = new Alert(Alert.AlertType.INFORMATION,
                            "注册成功", new ButtonType("确定", ButtonBar.ButtonData.YES));
                    alert.setHeaderText(null);
                    alert.setTitle("提示");
                    alert.show();
                    Stage stage = (Stage) SignUp_fxml.getScene().getWindow();
                    stage.close();

                    // ID.setText(IdText); 初始化账号值，后面想办法实现

                    //新建用户对象
                    UserInfo User = new UserInfo(IdText);
                    User.setPname(NicknameText);
                    try {
                        FileOutputStream fileOut =
                                new FileOutputStream("src/UserPosts/userObjects/" + IdText + ".ser");
                        ObjectOutputStream out = new ObjectOutputStream(fileOut);
                        out.writeObject(User);
                        out.close();
                        fileOut.close();
                    }catch(IOException i) {
                        i.printStackTrace();
                    }
                }
            }
            else {
                // 密码输入有误，弹出提示
                Alert alert = new Alert(Alert.AlertType.INFORMATION,
                        "两次输入密码不一致,请重新输入",
                        new ButtonType("确定", ButtonBar.ButtonData.YES));
                alert.setHeaderText(null);
                alert.setTitle("提示");
                alert.show();
                confirmPassword.setText("");
            }
        }
    }
}
